using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using Gj.Galaxy.Logic;

namespace Gj
{
    [InitializeOnLoad]
    public class EditorWatch
    {
        #region Public Attributes  

        #endregion

        #region Private Attributes

        private static bool firstPlay = true;
        public delegate void OnEventDelegate();
        public delegate void OnBoolEventDelegate(bool b);

        public static OnEventDelegate OnRefresh;
        public static OnEventDelegate OnAfterStop;
        public static OnEventDelegate OnPause;
        public static OnBoolEventDelegate OnBeforePlay;
        #endregion

        #region Public Methods  

        static EditorWatch()
        {
            EditorApplication.playModeStateChanged += DispatchEdit;
            EditorApplication.pauseStateChanged += DispatchPlay;
            EditorApplication.quitting += OnEditorQuitting;
        }

        static void DispatchEdit(PlayModeStateChange state)
        {
            switch (state)
            {
                case PlayModeStateChange.EnteredEditMode:
                    if (OnAfterStop != null) OnAfterStop();
                    ConnectManager.CleanupOnEditorQuit();
                    break;
                case PlayModeStateChange.EnteredPlayMode:
                case PlayModeStateChange.ExitingEditMode:
                    if (OnBeforePlay != null && !EditorApplication.isPlaying) OnBeforePlay(firstPlay);
                    if (firstPlay) firstPlay = false;
                    break;
                case PlayModeStateChange.ExitingPlayMode:
                    break;
            }
        }

        static void DispatchPlay(PauseState state)
        {
            switch (state)
            {
                case PauseState.Paused:
                    if (OnPause != null) OnPause();
                    break;
                case PauseState.Unpaused:
                    break;
            }
        }

        static void OnEditorQuitting()
        {
            Debug.Log("[EditorWatch] Unity编辑器正在关闭，清理网络连接...");
            ConnectManager.CleanupOnEditorQuit();
        }

        [MenuItem("Gj/Refresh")]
        public static void Refresh()
        {
            if (OnRefresh != null) OnRefresh();
        }

        [MenuItem("Gj/ClearApiCache")]
        public static void ClearApiCache()
        {
            CacheTools.RemovePrefix("api");
        }

        [MenuItem("Gj/ClearAllCache")]
        public static void ClearCache()
        {
            CacheTools.Clear();
        }

        [MenuItem("Gj/DownloadData")]
        public static void DownloadData()
        {
            Project.Base();
            var e = Manager.DownloadDataFile();
            while (e.MoveNext())
            {

            }
        }
        [MenuItem("Gj/DownloadDataToStream")]
        public static void DownloadDataToStream()
        {
            Project.Base();
            var e = Manager.DownloadDataZip();
            while (e.MoveNext())
            {

            }
        }

        [MenuItem("Gj/DownloadManagerData")]
        public static void DownloadManagerData()
        {
            Project.Base();
            var e = Manager.DownloadManagerDataFile();
            while (e.MoveNext())
            {

            }
        }
        #endregion

        #region Override Methods  

        #endregion

        #region Private Methods  
        public static T[] GetAllImplementTypes<T>(System.AppDomain aAppDomain) where T : class
        {
            var result = new List<T>();
            var assemblies = aAppDomain.GetAssemblies();
            foreach (var assembly in assemblies)
            {
                var types = assembly.GetTypes();
                foreach (var type in types)
                {
                    if (typeof(T).IsAssignableFrom(type))
                    {
                        if (!type.IsAbstract)
                        {
                            var tar = assembly.CreateInstance(type.FullName) as T;
                            result.Add(tar);
                        }
                    }
                }
            }
            return result.ToArray();
        }
        #endregion
    }
}
